/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tanks;

import tanks.Controllers.Controller;
import tanks.weapons.Weapon;
import tanks.weapons.Gun;
import com.sun.lwuit.Image;
import com.sun.lwuit.*;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author Александр
 */
public class Tank {

    int walkFrame = 0;
    int walkDuration = 3;
    int reloadFrame = 0;
    Controller controlStrategy;
    Vector weaps = new Vector();
    int currWeapIndex = 0;

    public Weapon CurrWeap() {
        return (Weapon) weaps.elementAt(currWeapIndex);
    }

    public int X() {
        if (CurrentCell != null) {
            return CurrentCell.X();
        } else {
            return -1;
        }
    }

    public void setX(int value) {
        if ((value >= 0 && value <= game.fieldWidth - 1) && game.Grid[value][Y()].Block == BlockType.Empty && game.Grid[value][Y()].currTank == null) {
            CurrentCell.currTank = null;
            CurrentCell = game.Grid[value][Y()];
            CurrentCell.currTank = this;
        }
    }

    public int Y() {
        if (CurrentCell != null) {
            return CurrentCell.Y();
        } else {
            return -1;
        }
    }

    public void setY(int value) {
        if ((value >= 0 && value <= game.fieldHeight - 1) && game.Grid[X()][value].Block == BlockType.Empty && game.Grid[X()][value].currTank == null) {
            CurrentCell.currTank = null;
            CurrentCell = game.Grid[X()][value];
            CurrentCell.currTank = this;
        }
    }
    Image Base;
    Game game = Game.Instance();
    public Cell CurrentCell;
    int health = 100;

    public int Health() {
        return health;
    }

    public void setHealth(int value) {
        health = value;
        System.out.println(health+" Health");
        if (value < 0) {
            game.Explode(4, CurrentCell,10);
            CurrentCell.currTank = null;
            game.RemoveTank(this);
        }
    }
    int facing = Direction.East;

    public int Facing() {
        return facing;
    }

    public Tank(int x, int y, Controller C) {
        controlStrategy = C;
        controlStrategy.tank = this;
        CurrentCell = game.Grid[x][y];
        CurrentCell.currTank = this;
        weaps.addElement(new Gun(this));
        try {
            Base = Image.createImage("/images/tank.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        //Base.SetResolution(game.Resolution, game.Resolution);
        game.Grid[x][y].Block = BlockType.Empty;
        setX(x);
        setY(y);
    }

    public void Fire() {
        if (reloadFrame <= 0) {
            reloadFrame = CurrWeap().Reload();
            CurrWeap().Fire();
        }
    }

    public boolean AddWeap(Weapon w) {
        boolean find = false;
        for (int i = 0; i < weaps.size(); i++) {
            Weapon wi = (Weapon) weaps.elementAt(i);
            if (wi.type == w.type) {
                find = true;
                break;
            }
        }
        if (!find) {
            weaps.addElement(w);
            currWeapIndex = weaps.size() - 1;
        }
        return (!find);
    }

    public void nextWeap() {
        currWeapIndex++;
        if (currWeapIndex > weaps.size() - 1) {
            currWeapIndex = 0;
        }
    }

    public void Step(int dir) {
        facing = dir;
        if (walkFrame <= 0) {
            walkFrame = walkDuration;
            switch (dir) {
                case Direction.North:
                    setY(Y() - 1);
                    break;
                case Direction.South:
                    setY(Y() + 1);
                    break;
                case Direction.West:
                    setX(X() - 1);
                    break;
                case Direction.East:
                    setX(X() + 1);
                    break;
            }
        }
    }

    public void Draw(Image canvas) {
        if (walkFrame > 0) {
            walkFrame--;
        }
        if (reloadFrame > 0) {
            reloadFrame--;
        }
        controlStrategy.Control();
        Graphics gr = canvas.getGraphics();
        Image Base1 = Base.scaled(Base.getWidth(), Base.getHeight());
        Image GunGr = CurrWeap().Gr.scaled(CurrWeap().Gr.getWidth(), CurrWeap().Gr.getHeight());
        switch (facing) {
            case Direction.North:
                Base1 = Base1.rotate(-90);
                GunGr = GunGr.rotate(-90);
                break;
            case Direction.South:
                Base1 = Base1.rotate(90);
                GunGr = GunGr.rotate(90);
                break;
            case Direction.West:
                Base1 = Base1.rotate(90);
                Base1 = Base1.rotate(90);
                GunGr = GunGr.rotate(90);
                GunGr = GunGr.rotate(90);
                break;
        }
        gr.drawImage(Base1, X() * Graph.CellSize(), Y() * Graph.CellSize(), Graph.CellSize(), Graph.CellSize());
        gr.drawImage(GunGr, X() * Graph.CellSize(), Y() * Graph.CellSize(), Graph.CellSize(), Graph.CellSize());

    }
}